/**
 * @description 对象转url参数
 * @param {object} data,对象
 * @param {Boolean} isPrefix,是否自动加上"?"
 * @param {string} arrayFormat 规则 indices|brackets|repeat|comma
 */
export const queryParams = (data = {}, isPrefix = true, arrayFormat = "brackets") => {
	const prefix = isPrefix ? "?" : "";
	const _result = [];
	if (["indices", "brackets", "repeat", "comma"].indexOf(arrayFormat) == -1) arrayFormat = "brackets";
	for (const key in data) {
		const value = data[key];
		// 去掉为空的参数
		if (["", undefined, null].indexOf(value) >= 0) {
			continue;
		}
		// 如果值为数组，另行处理
		if (value.constructor === Array) { // e.g. {ids: [1, 2, 3]}
			switch (arrayFormat) {
				case "indices":
					// 结果: 
					ids[0] = 1 & ids[1] = 2 & ids[2] = 3
					for (let i = 0; i < value.length; i++) {
						_result.push(`${key}[${i}]=${value[i]}`);
					}
					break;
				case "brackets":
					// 结果: 
					ids[] = 1 & ids[] = 2 & ids[] = 3 value.forEach((_value) => {
						_result.push(`${key}[]=${_value}`);
					});
					break;
				case "repeat":
					// 结果: 
					ids = 1 & ids = 2 & ids = 3 value.forEach((_value) => {
						_result.push(`${key}=${_value}`);
					});
					break;
				case "comma":
					// 结果:
					ids = 1, 2, 3
					let commaStr = "";
					value.forEach((_value) => {
						commaStr += (commaStr ? "," : "") + _value;
					});
					_result.push(`${key}=${commaStr}`);
					break;
				default:
					value.forEach((_value) => {
						_result.push(`${key}[]=${_value}`);
					});
			}
		} else {
			_result.push(`${key}=${value}`);
		}
	}
	return _result.length ? prefix + _result.join("&") : "";
};

/**
 * 是否数组
 */
function isArray(value) {
	if (typeof Array.isArray === "function") {
		return Array.isArray(value);
	}
	return Object.prototype.toString.call(value) === "[object Array]";
}

/**
 * @description 深度克隆
 * @param {object} obj 需要深度克隆的对象
 * @returns {*} 克隆后的对象或者原值（不是对象）
 */
export const deepClone = (
obj) => { // 对常见的“非”值，直接返回原来值if ([null, undefined, NaN, false].includes(obj)) return obj;if (typeof obj !== "object" && typeof obj !== "function") {// 原始类型直接返回return obj;}const o = isArray(obj) ? [] : {};for (const i in obj) {if (obj.hasOwnProperty(i)) {o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];}}return o;
};

/**
 * @description JS对象深度合并
 * @param {object} target 需要拷贝的对象
 * @param {object} source 拷贝的来源对象
 * @returns {object|boolean} 深度合并后的对象或者false（入参有不是对象）
 */
export const deepMerge = (target = {}, source = {}) => {
	target = deepClone(target);
	if (typeof target !== "object" || typeof source !== "object") return false;
	for (const prop in source) {
		if (!source.hasOwnProperty(prop)) continue;
		if (prop in target) {
			if (typeof target[prop] !== "object") {
				target[prop] = source[prop];
			} else if (typeof source[prop] !== "object") {
				target[prop] = source[prop];
			} else if (target[prop].concat && source[prop].concat) {
				target[prop] = target[prop].concat(source[prop]);
			} else {
				target[prop] = deepMerge(target[prop], source[prop]);
			}
		} else {
			target[prop] = source[prop];
		}
	}
	return target;
};

/**
 * @description 获取当前页面路径
 */
export const page = () => {
	const pages =
getCurrentPages(); // 某些特殊情况下(比如页面进行redirectTo时的一些时机)，pages可能为空数组return `/${pages[pages.length - 1]?.route ?? ""}`;
};
